import { nextTick, Transition, h, defineComponent, ref, onMounted } from 'vue';
import { keep } from '../../_utils';
import NNotification, { notificationProps, notificationPropKeys } from './Notification';
export const notificationEnvOptions = Object.assign(Object.assign({}, notificationProps), { duration: Number, onClose: Function, onLeave: Function, onAfterEnter: Function, onAfterLeave: Function, 
    /** @deprecated */
    onHide: Function, 
    /** @deprecated */
    onAfterShow: Function, 
    /** @deprecated */
    onAfterHide: Function });
export default defineComponent({
    name: 'NotificationEnvironment',
    props: Object.assign(Object.assign({}, notificationEnvOptions), { 
        // private
        internalKey: {
            type: String,
            required: true
        }, onInternalAfterLeave: {
            type: Function,
            required: true
        } }),
    setup(props) {
        const showRef = ref(true);
        let timerId = null;
        function hide() {
            showRef.value = false;
            if (timerId) {
                window.clearTimeout(timerId);
            }
        }
        function handleBeforeEnter(el) {
            void nextTick(() => {
                el.style.height = `${el.offsetHeight}px`;
                el.style.maxHeight = '0';
                el.style.transition = 'none';
                void el.offsetHeight;
                el.style.transition = '';
                el.style.maxHeight = el.style.height;
            });
        }
        function handleAfterEnter(el) {
            el.style.height = '';
            el.style.maxHeight = '';
            const { onAfterEnter, onAfterShow } = props;
            if (onAfterEnter)
                onAfterEnter();
            // deprecated
            if (onAfterShow)
                onAfterShow();
        }
        function handleBeforeLeave(el) {
            el.style.maxHeight = `${el.offsetHeight}px`;
            el.style.height = `${el.offsetHeight}px`;
            void el.offsetHeight;
        }
        function handleLeave(el) {
            const { onHide } = props;
            if (onHide)
                onHide();
            el.style.maxHeight = '0';
            void el.offsetHeight;
        }
        function handleAfterLeave() {
            const { onAfterLeave, onInternalAfterLeave, onAfterHide, internalKey } = props;
            if (onAfterLeave)
                onAfterLeave();
            onInternalAfterLeave(internalKey);
            // deprecated
            if (onAfterHide)
                onAfterHide();
        }
        function handleClose() {
            const { onClose } = props;
            if (onClose) {
                void Promise.resolve(onClose()).then((feedback) => {
                    if (feedback === false)
                        return;
                    hide();
                });
            }
            else {
                hide();
            }
        }
        onMounted(() => {
            if (props.duration) {
                timerId = window.setTimeout(hide, props.duration);
            }
        });
        return {
            show: showRef,
            hide,
            handleClose,
            handleAfterLeave,
            handleLeave,
            handleBeforeLeave,
            handleAfterEnter,
            handleBeforeEnter
        };
    },
    render() {
        return (h(Transition, { name: "notification-transition", appear: true, 
            // convert to any since Element is not compitable with HTMLElement
            onBeforeEnter: this.handleBeforeEnter, onAfterEnter: this.handleAfterEnter, onBeforeLeave: this.handleBeforeLeave, onLeave: this.handleLeave, onAfterLeave: this.handleAfterLeave }, {
            default: () => {
                return this.show ? (h(NNotification, Object.assign({}, keep(this.$props, notificationPropKeys), { onClose: this.handleClose }))) : null;
            }
        }));
    }
});
